﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.HVAC;
using YArchitech.Revit;

namespace HYCreateDuct.MethodCreateDuct
{
	internal class GetDuctPara
	{
		public static List<systemTypeStruct> GetCurrentSystemSetDat(Autodesk.Revit.DB.Document doc)
		{
			List<systemTypeStruct> list = new List<systemTypeStruct>();
			try
			{
				List<MechanicalSystemType> list2 = GetDuctPara.Get_MechanicalSystemType(doc);
				for (int i = 0; i < list2.Count; i++)
				{
					if (list2[i] != null)
					{
						string text = GetDuctPara.SetMechanicalSystemSimple(list2[i]);
						systemTypeStruct systemTypeStruct = new systemTypeStruct();
						systemTypeStruct.PipingSystemType = AssistSystemType.GetDuctSystemTypeCH(list2[i].Name);
						systemTypeStruct.PipingSystemTypeAbb = list2[i].get_Parameter(BuiltInParameter.RBS_SYSTEM_ABBREVIATION_PARAM).AsString();
						if (string.IsNullOrEmpty(systemTypeStruct.PipingSystemTypeAbb) && !string.IsNullOrEmpty(text))
						{
							systemTypeStruct.PipingSystemTypeAbb = text;
						}
						systemTypeStruct.PipeSystemType = list2[i].SystemClassification.ToString();
						systemTypeStruct.Color = GetDuctPara.getRGB(list2[i]);
						systemTypeStruct.LinePattern = GetDuctPara.getLinePattern(list2[i], doc);
						systemTypeStruct.LineWeight = GetDuctPara.getLineWeight(list2[i]);
						systemTypeStruct.SystemClass = GetDuctPara.GetSystemClassnew(list2[i].SystemClassification.ToString());
						list.Add(systemTypeStruct);
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
			return list;
		}

		private static List<Duct> FindAllDuct(Autodesk.Revit.DB.Document doc)
		{
			List<Duct> list = new List<Duct>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(Duct)).OfCategory(BuiltInCategory.OST_DuctCurves);
			foreach (Element element in filteredElementCollector)
			{
				Duct duct = element as Duct;
				if (duct != null)
				{
					list.Add(duct);
				}
			}
			return list;
		}

		public static bool GetBoolSystemType(Autodesk.Revit.DB.Document doc, string SystemType)
		{
			using (List<Duct>.Enumerator enumerator = GetDuctPara.FindAllDuct(doc).GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.GetParameter("系统类型").AsValueString().Equals(SystemType))
					{
						return true;
					}
				}
			}
			return false;
		}

		public static System.Drawing.Color getXMLDataColor(string rgb)
		{
			System.Drawing.Color result = default(System.Drawing.Color);
			if (rgb != "默认")
			{
				result = System.Drawing.Color.FromArgb(int.Parse(rgb.Split(new char[]
				{
					' '
				}).ElementAt(1)), int.Parse(rgb.Split(new char[]
				{
					' '
				}).ElementAt(2)), int.Parse(rgb.Split(new char[]
				{
					' '
				}).ElementAt(3)));
			}
			else
			{
				result = System.Drawing.Color.FromArgb(128, 128, 128);
			}
			return result;
		}

		public static int GetIntAboutColor(System.Drawing.Color color)
		{
			return (int)color.B << 16 | (int)((ushort)((int)color.G << 8 | (int)color.R));
		}

		public static List<LinePatternElement> Get_LinePatternElement(Autodesk.Revit.DB.Document document)
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(document).OfClass(typeof(LinePatternElement)).ToElements();
			List<LinePatternElement> list = new List<LinePatternElement>();
			foreach (Element element in enumerable)
			{
				LinePatternElement item = element as LinePatternElement;
				list.Add(item);
			}
			return list;
		}

		private static string getLineWeight(MechanicalSystemType pst)
		{
			string result;
			if (pst.LineWeight == 0 || pst.LineWeight == 1 || pst.LineWeight == 2 || pst.LineWeight == 3 || pst.LineWeight == 4 || pst.LineWeight == 5 || pst.LineWeight == 6 || pst.LineWeight == 7 || pst.LineWeight == 8 || pst.LineWeight == 9 || pst.LineWeight == 10 || pst.LineWeight == 11 || pst.LineWeight == 12 || pst.LineWeight == 13 || pst.LineWeight == 14 || pst.LineWeight == 15 || pst.LineWeight == 16)
			{
				result = pst.LineWeight.ToString();
			}
			else
			{
				result = "默认";
			}
			return result;
		}

		private static string getLinePattern(MechanicalSystemType pst, Autodesk.Revit.DB.Document doc)
		{
			string result = null;
			if (pst.LinePatternId.IntegerValue != -1)
			{
				try
				{
					return LinePatternElement.GetLinePattern(doc, pst.LinePatternId).Name;
				}
				catch
				{
					return "默认";
				}
			}
			result = "默认";
			return result;
		}

		public static string getNewRGB(byte R)
		{
			string result = "";
			if (R <= 9)
			{
				result = "00" + R.ToString();
			}
			else if (R <= 99 && R >= 10)
			{
				result = "0" + R.ToString();
			}
			if (R >= 100)
			{
				result = R.ToString();
			}
			return result;
		}

		private static string getRGB(MechanicalSystemType mst)
		{
			string result;
			if (mst.LineColor.IsValid)
			{
				string newRGB = GetDuctPara.getNewRGB(mst.LineColor.Red);
				string newRGB2 = GetDuctPara.getNewRGB(mst.LineColor.Green);
				string newRGB3 = GetDuctPara.getNewRGB(mst.LineColor.Blue);
				result = string.Concat(new string[]
				{
					"█RGB ",
					newRGB.ToString(),
					" ",
					newRGB2.ToString(),
					" ",
					newRGB3.ToString()
				});
			}
			else
			{
				result = "█RGB 128 128 128";
			}
			return result;
		}

		private static string GetSystemClassnew(string pipeSystemType)
		{
			uint num = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(pipeSystemType);
			if (num <= 2017692542u)
			{
				if (num <= 1524890971u)
				{
					if (num <= 514019971u)
					{
						if (num != 81264407u)
						{
							if (num == 514019971u)
							{
								if (pipeSystemType == "ReturnHydronic")
								{
									return "管道系统";
								}
							}
						}
						else if (pipeSystemType == "ExhaustAir")
						{
							return "风管系统";
						}
					}
					else if (num != 842668758u)
					{
						if (num == 1524890971u)
						{
							if (pipeSystemType == "DomesticHotWater")
							{
								return "管道系统";
							}
						}
					}
					else if (pipeSystemType == "SupplyHydronic")
					{
						return "管道系统";
					}
				}
				else if (num <= 1698412355u)
				{
					if (num != 1678934222u)
					{
						if (num == 1698412355u)
						{
							if (pipeSystemType == "FireProtectPreaction")
							{
								return "管道系统";
							}
						}
					}
					else if (pipeSystemType == "Global")
					{
						return "全局";
					}
				}
				else if (num != 1807552510u)
				{
					if (num != 2003677185u)
					{
						if (num == 2017692542u)
						{
							if (pipeSystemType == "Vent")
							{
								return "管道系统";
							}
						}
					}
					else if (pipeSystemType == "FireProtectDry")
					{
						return "管道系统";
					}
				}
				else if (pipeSystemType == "Sanitary")
				{
					return "管道系统";
				}
			}
			else if (num <= 3017983553u)
			{
				if (num <= 2482186404u)
				{
					if (num != 2382714854u)
					{
						if (num == 2482186404u)
						{
							if (pipeSystemType == "FireProtectWet")
							{
								return "管道系统";
							}
						}
					}
					else if (pipeSystemType == "DomesticColdWater")
					{
						return "管道系统";
					}
				}
				else if (num != 2603360416u)
				{
					if (num == 3017983553u)
					{
						if (pipeSystemType == "ReturnAir")
						{
							return "风管系统";
						}
					}
				}
				else if (pipeSystemType == "Fitting")
				{
					return "管件";
				}
			}
			else if (num <= 3891676362u)
			{
				if (num != 3313474090u)
				{
					if (num == 3891676362u)
					{
						if (pipeSystemType == "UndefinedSystemType")
						{
							return "管道系统";
						}
					}
				}
				else if (pipeSystemType == "FireProtectOther")
				{
					return "管道系统";
				}
			}
			else if (num != 3995932714u)
			{
				if (num != 4199155463u)
				{
					if (num == 4225100969u)
					{
						if (pipeSystemType == "OtherPipe")
						{
							return "管道系统";
						}
					}
				}
				else if (pipeSystemType == "OtherAir")
				{
					return "风管系统";
				}
			}
			else if (pipeSystemType == "SupplyAir")
			{
				return "风管系统";
			}
			return "管道系统";
		}

		public static string SetMechanicalSystemSimple(MechanicalSystemType mst)
		{
			string text = "";
			string text2 = mst.Name;
			if (!(text2 == "送风"))
			{
				if (!(text2 == "新风"))
				{
					if (!(text2 == "回风"))
					{
						if (!(text2 == "排风"))
						{
							if (!(text2 == "排烟"))
							{
								if (text2 == "加压送风")
								{
									text = "JY";
								}
							}
							else
							{
								text = "PY";
							}
						}
						else
						{
							text = "PF";
						}
					}
					else
					{
						text = "HF";
					}
				}
				else
				{
					text = "XF";
				}
			}
			else
			{
				text = "SF";
			}
			try
			{
				Parameter parameter = mst.get_Parameter(BuiltInParameter.RBS_SYSTEM_ABBREVIATION_PARAM);
				if (parameter.AsString() == "")
				{
					parameter.Set(text);
				}
				text2 = text;
			}
			catch (Exception)
			{
				text2 = text;
			}
			return text2;
		}

		public static List<MechanicalSystemType> Get_MechanicalSystemType(Autodesk.Revit.DB.Document document)
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(document).OfClass(typeof(MechanicalSystemType)).ToElements();
			List<MechanicalSystemType> list = new List<MechanicalSystemType>();
			foreach (Element element in enumerable)
			{
				MechanicalSystemType item = element as MechanicalSystemType;
				list.Add(item);
			}
			return list;
		}

		public static DuctWindowStruct GetDuctParameter(Autodesk.Revit.DB.Document doc, Duct duct)
		{
			DuctWindowStruct ductWindowStruct = new DuctWindowStruct();
			new YJKPiping<Duct>(duct);
			Element element = doc.GetElement(duct.Id);
			Parameter parameter = element.GetParameter(BuiltInParameter.RBS_DUCT_SYSTEM_TYPE_PARAM);
			Parameter parameter2 = element.GetParameter(BuiltInParameter.ELEM_FAMILY_PARAM);
			ductWindowStruct.dDuctFlow = 1000.0;
			Parameter parameter3 = element.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM);
			Parameter parameter4 = element.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM);
			Parameter parameter5 = element.GetParameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM);
			Parameter parameter6 = element.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM);
			Parameter parameter7 = element.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM);
			Parameter parameter8 = element.GetParameter(BuiltInParameter.RBS_CURVE_HOR_OFFSET_PARAM);
			Parameter parameter9 = element.GetParameter(BuiltInParameter.RBS_CURVE_VERT_OFFSET_PARAM);
			Parameter parameter10 = element.GetParameter(BuiltInParameter.RBS_DUCT_BOTTOM_ELEVATION);
			Parameter parameter11 = element.GetParameter(BuiltInParameter.RBS_DUCT_TOP_ELEVATION);
			string sp = "中心";
			string cz = "中";
			if (parameter != null)
			{
				ductWindowStruct.strSystemType = parameter.AsValueString();
			}
			if (parameter2 != null)
			{
				ductWindowStruct.strDuctType = parameter2.AsValueString();
				ElementId elementId = parameter2.AsElementId();
				Element element2 = doc.GetElement(elementId);
				ductWindowStruct.strDuctTypeName = ductWindowStruct.strDuctType + "-" + element2.Name;
			}
			if (parameter3 != null)
			{
				ductWindowStruct.dDuctWidth = parameter3.AsDouble() * 304.8;
			}
			if (parameter4 != null)
			{
				ductWindowStruct.dDuctHeight = parameter4.AsDouble() * 304.8;
			}
			if (parameter5 != null)
			{
				ductWindowStruct.dDuctDiameter = parameter5.AsDouble() * 304.8;
			}
			if (parameter6 != null)
			{
				ductWindowStruct.strLevel = parameter6.AsValueString();
			}
			if (parameter7 != null)
			{
				ductWindowStruct.dOffset = parameter7.AsDouble() * 304.8;
			}
			if (parameter8 != null)
			{
				sp = parameter8.AsValueString();
			}
			if (parameter9 != null)
			{
				cz = parameter9.AsValueString();
			}
			if (parameter10 != null)
			{
				ductWindowStruct.dSOffset = parameter10.AsDouble() * 304.8;
			}
			if (parameter11 != null)
			{
				ductWindowStruct.dEOffset = parameter11.AsDouble() * 304.8;
			}
			ductWindowStruct.nStyle = GetDuctPara.GetNType(sp, cz);
			ductWindowStruct.nHorizontalOffset = 0.0;
			return ductWindowStruct;
		}

		public static int GetNType(string sp, string cz)
		{
			int result;
			if (sp.Equals("左") && cz.Equals("顶"))
			{
				result = 1;
			}
			else if (sp.Equals("中心") && cz.Equals("顶"))
			{
				result = 2;
			}
			else if (sp.Equals("右") && cz.Equals("顶"))
			{
				result = 3;
			}
			else if (sp.Equals("左") && cz.Equals("中"))
			{
				result = 4;
			}
			else if (sp.Equals("中心") && cz.Equals("中"))
			{
				result = 5;
			}
			else if (sp.Equals("右") && cz.Equals("中"))
			{
				result = 6;
			}
			else if (sp.Equals("左") && cz.Equals("底"))
			{
				result = 7;
			}
			else if (sp.Equals("中心") && cz.Equals("底"))
			{
				result = 8;
			}
			else if (sp.Equals("右") && cz.Equals("底"))
			{
				result = 9;
			}
			else
			{
				result = 0;
			}
			return result;
		}
	}
}
